<?

	include("inc/config.inc.php");
    
    include("inc/header.inc.php");

	$edit = mysqli_real_escape_string($con,$_GET[e]);

	// Check we are HR!
	if ($_SESSION[role] != "HR")
		die ("Error 71 - User error - Please contact support.");

	echo "<h2><span class='textColorChange'>$config[systemtitle]</span></h2><br/>\n\n";

        echo "<a href='main.php' style='color:black;font-size:10pt'><< Back</a><br/>";

        echo "<div style='background:#504E52' class='normalText' width='400'>\n";

	echo "<b>Manage Compulsory Holidays</b><br/><br/>\n";
	echo "Any dates added here will be listed and deducted for every employee.<br/><br/>\n";

	// Get all users, show prestages ones first as they require attention
	$result = mysqli_query($conn, "SELECT * FROM requests WHERE user='' ORDER BY startdate DESC");
	if (mysqli_num_rows($result) > 0)
	{

		// Form validation
		echo "<script>
		function validateForm()
		{
			if (document.forms['form']['requestid'].value == '' || document.forms['form']['date'].value == '' || document.forms['form']['name'].value == '')
			{
				alert('Please fill in all fields!');
				return false;
			}
		}

		$(function() {
		$('#startdatepicker').datepicker({
			dateFormat: 'yy-mm-dd'
		});
		$(function() {
		$('#enddatepicker').datepicker({
			dateFormat: 'yy-mm-dd'
		});

		});




		</script>
		";
				

		echo "<table BORDERCOLORLIGHT='#807E7C' BORDERCOLORDARK='#383634' border='1'><tr><td>Start Date</td><td>End Date</td><td>Description</td><td>Action</td></td></tr>";
		
		//Not editing, show first row as insert row
		if ($edit == "")
		{
			echo "<form name='form' action='updatechol.php' method='POST' onsubmit='return validateForm()'><input type='hidden' name='new' value='true'>";
			echo "<tr><td><input type='text' name='startdate' id='startdatepicker'></td><td><input type='text' name='enddate' id='enddatepicker'></td><td><input type='text' name='name'></td><td><input type='submit' value='Save'></td></tr>";
			echo "</form>";
		}
		
		while ($b = mysqli_fetch_array($result))
		{

				echo "<tr>\n";

			// Row for editing
			if ($edit == $b[requestid])
			{
				echo "<form name='form' action='updatechol.php' method='POST' onsubmit='return validateForm()'>";
				echo "<input type='hidden' name='requestid' value='$b[requestid]'>\n";
				echo "<td><input type='text' name='startdate' value='$b[startdate]' id='startdatepicker'></td>\n";
				echo "<td><input type='text' name='enddate' value='$b[enddate]' id='enddatepicker'></td>\n";
				echo "<td><input type='text' name='name' value='$b[description]'></td>\n";
			}
			
			// Regular view row
			else
			{
				echo "<td>$b[startdate]</td>\n";
				echo "<td>$b[enddate]</td>\n";
				echo "<td>$b[description]</td>\n";
			}

			// Actions
			if($edit == $b[requestid])
				echo "<td><input type='submit' value='Save'><br/><input type='button' value='Cancel' onClick='location.href=\"chol.php\"'></td></form></tr>\n";
			elseif ($edit=="")
			{
				echo "<td><a href='chol.php?e=$b[requestid]'><img src='img/application_edit.png' width='16' height='16' border='0'><small>Edit</small></a><br/>\n";
				echo "<a href='choldel.php?e=$b[requestid]' onClick='return confirmDelete(\"delete\");'><img src='img/delete.png' width='16' height='16' border='0'><small>Delete</small></a></td></tr>\n";
			}
			else
				echo "<td><br/><br/><br/></td></tr>\n";
				
		}
		echo "</table>\n";

	}

	echo "</div>\n";

	include("inc/footer.inc.php");
    
?>
    
